<?php 
$_SESSION[TINYMCE_UPLOAD] = 'upload/plugins/' . basename(dirname(__FILE__)) . '/';
if (!file_exists('../' . $_SESSION[TINYMCE_UPLOAD])) {
  Utils::create('../' . $_SESSION[TINYMCE_UPLOAD]);
}

$id = Utils::get_param('id', 0);
$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d;

$pg = Utils::get_param('pg', 1);
$objs = $orm->get('pb_ecom_customer', 't.account', 'ASC', $pg, SIZE, NULL);

$members = $orm->get('pb_ecom_membership', 't.total_spent', 'ASC', NULL, NULL, NULL)->d;
?>
<div class="pure-form pure-form-aligned" style="padding: 10px; width: 790px;">
  <legend>Customer</legend>
  
  <div class="pure-control-group">
    <label>Status</label>
    <select id="status" style="width: 200px;">
      <option value="0">Inactivated</option>
      <option value="1">Current</option>
      <option value="2">Suspended</option>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>Membership</label>
    <select id="member" style="width: 200px;">
      <option value="0">&nbsp;</option>
      <?php foreach ($members as $itm) { ?>
      <option value="<?php echo $itm->id; ?>"><?php echo $itm->title . ' (' . $itm->discount . ')'; ?></option>
      <?php } ?>
    </select>
  </div>
  
  <div class="pure-control-group">
    <label>Account</label>
    <input id="account" type="text" class="pure-input-1-2" placeholder="{Account}" />
  </div>
  
  <div class="pure-control-group">
    <label>Password</label>
    <input id="password" type="text" class="pure-input-1-2" placeholder="{Password}" />
  </div>
  
  <div class="pure-control-group">
    <label>First Name</label>
    <input id="first_name" type="text" class="pure-input-1-2" placeholder="{First Name}" />
  </div>
  
  <div class="pure-control-group">
    <label>Last Name</label>
    <input id="last_name" type="text" class="pure-input-1-2" placeholder="{Last Name}" />
  </div>
  
  <div class="pure-control-group">
    <label>Phone</label>
    <input id="phone" type="text" class="pure-input-1-2" placeholder="{Phone}" />
  </div>


  <div class="pure-control-group">
    <label>&nbsp;</label>
    <button class="pure-button pure-input-1-2 pure-button-primary" onclick="submit_obj();">Save</button>
  </div>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __members = JSON.parse('<?php echo addslashes(json_encode($members)); ?>');
  var __objs = JSON.parse('<?php echo addslashes(json_encode($objs)); ?>');
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __paypal = '<?php echo P_PAYPAL; ?>';
  
  $(function(){
    reset();
    
    render();
  });

  function reset() {
    if (__data.length == 0) {
      __data[0] = {
        id: 0,
        account: '',
        __password: '',
        first_name: '',
        last_name: '',
        phone: '',
        status: 1,
        member_id: 0
      };
    }
  };

  function render() {
    Utils.leftbar(
      __objs,
      __data[0].id,
      'Customers',
      'account',
      function(page) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=' + __file + '&id=' + __data[0].id + '&pg=' + page);
      }
    );
    render_data();
  };

  function render_data() {
    Url.insert_param('id', __data[0].id);
    
    $('#account').val(__data[0].account);
    $('#password').val('');
    $('#first_name').val(__data[0].first_name);
    $('#last_name').val(__data[0].last_name);
    $('#phone').val(__data[0].phone);
    $('#member').val(__data[0].member_id);
    $('#status').val(__data[0].status);
  };

  function submit_obj() {
    req = __data[0];
    req.account = $('#account').val();
    req.__password = $('#password').val();
    req.first_name = $('#first_name').val();
    req.last_name = $('#last_name').val();
    req.phone = $('#phone').val();
    req.status = $('#status option:selected').val();
    req.member_id = $('#member option:selected').val();
    req.__pg = __objs.p;

    Ajax.request(
      __requrl.replace(__file, 'index') + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        if (rslt.__objs) {
          __objs = rslt.__objs;
          delete rslt.__objs;
        }
        __data[0] = rslt;
        render();
      },
      null
    );
  };
  
  function get_obj(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'get_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>